//
//  GxNewFeatureCell.swift
//  microblog
//
//  Created by 记忆不为 on 16/9/30.
//  Copyright © 2016年 记忆不为. All rights reserved.
//

import UIKit

class GxNewFeatureCell: UICollectionViewCell {
    //cell的图片
    var indexPath: IndexPath? {
        didSet {
            
            backgroundImageView.image = UIImage(named: "new_feature_\(indexPath!.item + 1)")
            
            // 判断是最后一页才显示,其他页隐藏
            if indexPath?.item == NewFeatureCount - 1 {
                showButton(isHidden: false)
            } else {
                // 不是最后一页
                showButton(isHidden: true)
            }
        }
    }
    private func showButton(isHidden: Bool) {
        sharedButton.isHidden = isHidden
        startButton.isHidden = isHidden
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    func setup() -> Void {
        //添加子控件
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(sharedButton)
        contentView.addSubview(startButton)
        
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        sharedButton.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: startButton, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: startButton, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: -130))
        
        self.contentView.addConstraint(NSLayoutConstraint(item: sharedButton, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1, constant: 0))
        // bottom
        self.contentView.addConstraint(NSLayoutConstraint(item: sharedButton, attribute: .bottom, relatedBy: .equal, toItem: startButton, attribute: .top, multiplier: 1, constant: -16))
        
        
    }
    
    // MARK: - 懒加载
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var sharedButton : UIButton = {
        let button = UIButton()
        button.setTitle("分享给大家", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        
        // 图片
        button.setImage(UIImage(named: "new_feature_share_false"), for: .normal)
        button.setImage(UIImage(named: "new_feature_share_true"), for: .selected)
        
        button.addTarget(self, action: #selector(sharedButtonDidClick), for: .touchUpInside)
        return button
    }()
    
    private lazy var startButton : UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), for: .normal)
        button.setTitle("开始体验", for: .normal)
        button.addTarget(self, action: #selector(startButtonDidClick), for: .touchUpInside)
        return button
    }()
    
    // MARK: - 事件方法
    func sharedButtonDidClick() -> Void {
        sharedButton.isSelected = !sharedButton.isSelected
    }
    func startButtonDidClick() -> Void{
        UIApplication.shared.keyWindow?.rootViewController = GxTabBarController()
    }
    
}
